<template>
	<view class="content">
		<u-search placeholder="请输入关键词" v-model="keyword" :clearabled="true" :animation="true" height="30" margin="20rpx"
			@search="searchChange" @custom="searchChange"></u-search>
		<mescroll-body @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view v-for="data in searchList" style="margin-bottom: 16rpx;" @click="cashDetail(data._id)">
				<cashList :cashInfo="data" v-if="searchList.length">
				</cashList>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				keyword: null,
				searchList: [], //获取回来的
			}
		},
		onLoad(event) {
			console.log(typeof event.keyWord);
			event.keyWord ? this.keyword = event.keyWord : null

		},
		methods: {
			/*上拉加载的回调*/
			async upCallback(page) {
				// console.log(page);
				if (page.num === 1) this.searchList = []
				/* 获取数据*/
				let res = []
				if (this.keyword) {
					res = await uniCloud.callFunction({
						name: "getSearch",
						data: {
							keyword: this.keyword,
							flag: 1
						}
					})
					this.searchList = this.searchList.concat(res.result.data)
					// this.searchChange()
					this.mescroll.endSuccess(res.result.data.length);
				} else {
					this.mescroll.endSuccess(0);
				}

			},
			async searchChange() {
				console.log("searchChange搜索框事件");
				if (!this.keyword) return uni.showToast({
					title: "请输入搜索内容",
					icon: "none"
				})
				this.mescroll.resetUpScroll()
				this.setLocalStorage(this.keyword)
			},
			setLocalStorage(e) {
				if (!e) return
				let getStorageData = uni.getStorageSync("searchHistory")
				let SearchHistory = getStorageData ? JSON.parse(getStorageData) : Array()
				SearchHistory.unshift(e)
				uni.setStorageSync("searchHistory", JSON.stringify(SearchHistory))
			},
			cashDetail(event) {
				console.log("点击案例阅读页", event);
				uni.navigateTo({
					url: "/pages/cashRead/cashRead?cash_id=" + event
				})
			},
		}
	}
</script>

<style>
	.content {
		width: 100%;
		min-height: 100vh;
	}
</style>